﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConfigServices.LayeredConfig
{
    /// <summary>
    /// 可覆盖的配置读取器
    /// </summary>
    public class LayeredConfigReader : IConfigReader
    {
        //注入多个IConfigService
        private readonly IEnumerable<IConfigService> services;

        public LayeredConfigReader(IEnumerable<IConfigService> services)
        {
            this.services = services;
        }

        /// <summary>
        /// 如果配置文件找不到就返回null
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string GetValue(string name)
        {
            string value = null;
            //可覆盖的配置读取器:最简单的可覆盖的配置服务器
            foreach (var service in services)
            {
                string newValue = service.GetValue(name);
                if (newValue != null) //按顺序来读,只要有不为null的就会被覆盖
                {
                    value = newValue;  //最后一个不为null的值,就是最终值
                }
            }

            return value;


        }
    }
}
